package com.runa.protocol.util;

import com.alibaba.fastjson.JSON;
import com.runa.monitor.comm.dto.newComm.AffiliatePanelVO;
import com.runa.monitor.comm.dto.newComm.AsynResult;
import com.runa.monitor.comm.dto.newComm.PanelVO;
import com.runa.monitor.comm.dto.newComm.ValveVO;
import com.runa.monitor.comm.entity.CommPanel;
import com.runa.monitor.comm.entity.CommValve;
import com.runa.persistence.CollectionUtil;
import com.runa.persistence.StringUtil;
import com.runa.persistence.cache.CommPanelCache;
import com.runa.persistence.cache.CommValveCache;
import com.runa.persistence.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 2024/9/5 9:00
 * zhangYB
 */
@Slf4j
public class FaultUtil {

    private static CommValveCache commValveCache = SpringContextUtil.getBean(CommValveCache.class);
    private static CommPanelCache commPanelCache = SpringContextUtil.getBean(CommPanelCache.class);

    private static void addValveSeparateWithBody(List<String> faultList, String model) {
        if (!model.contains("执行器")) {
            faultList.add("阀体分离");
        }
    }

    private static void analysisValve(AsynResult asynResult, ValveVO valveVO) {
        List<String> faultCode = valveVO.getFaultCode();
        if (CollectionUtil.isEmpty(faultCode) || !(faultCode.contains("阀体分离") || faultCode.contains("无线异常"))) {
            log.debug("analysisValve 阀 {} faultCode: {}", asynResult.getIdentity(), faultCode);
            return;
        }
        CommValve commValve = commValveCache.getCommValveById(asynResult.getIdentity());
        if (null == commValve) {
            log.debug("analysisValve 阀 {} getCommValveById is null", asynResult.getIdentity());
            return;
        }
        log.debug("analysisValve 阀 {} commValve: {}", asynResult.getIdentity(), JSON.toJSONString(commValve));
        String model = commValve.getModel();
        if (StringUtil.isNotBlank(model)) {
            if (model.contains("执行器")) {
                faultCode.remove("阀体分离");
            }
            if (model.contains("ABN")) {
                faultCode.remove("无线异常");
            }
        }
    }

    /**
     * 阀下面板过滤型号ABN的故障：无线异常
     * @param valveVO
     */
    private static void analysisValvePanel(ValveVO valveVO) {
        List<AffiliatePanelVO> panelVOList = valveVO.getPanelVOList();
        if (CollectionUtil.isEmpty(panelVOList)) {
            return;
        }
        for (AffiliatePanelVO affiliatePanelVO : panelVOList) {
            analysisPanel(affiliatePanelVO.getFaultCode(), affiliatePanelVO.getId());
        }
    }


    private static void analysisPanel(List<String> faultCode, String panelId) {
        if (CollectionUtil.isEmpty(faultCode)) {
            log.debug("面板 {} faultCode is null", panelId);
            return;
        }
        if (!faultCode.contains("无线异常")) {
            log.debug("面板 {} faultCode 不包含无线异常", panelId);
            return;
        }
        CommPanel commPanel = commPanelCache.getCommPanelById(panelId);
        if (null == commPanel) {
            log.debug("面板 {} getCommPanelById is null", panelId);
            return;
        }
        log.debug("面板 {} {}", panelId, JSON.toJSONString(commPanel));
        String model = commPanel.getModel();
        if (StringUtil.isNotBlank(model) && model.contains("ABN")) {
            faultCode.remove("无线异常");
        }
    }

    private static void analysisPanel(AsynResult asynResult) {
        Object content = asynResult.getContent();
        if (content instanceof List) {
            List list = (List)content;
            for (Object o : list) {
                if (o instanceof PanelVO) {
                    PanelVO panelVO = (PanelVO)o;
                    analysisPanel(panelVO.getFaultCode(), asynResult.getIdentity());
                }
            }
        }
    }

    private static void analysisValve(AsynResult asynResult) {
        Object content = asynResult.getContent();
        if (content instanceof ValveVO) {
            ValveVO valveVO = (ValveVO)content;
            analysisValve(asynResult, valveVO);
            analysisValvePanel(valveVO);
        }
    }

    public static void analysis(String topic, AsynResult asynResult) {
        try {
            if (null == asynResult) {
                return;
            }
            if (null == asynResult.getContent()) {
                return;
            }
            log.debug("过滤前: {} {}", topic, asynResult);
            if (ValveVO.TOPIC.equals(topic)) {
                analysisValve(asynResult);
            } else if (PanelVO.TOPIC.equals(topic)) {
                analysisPanel(asynResult);
            }
            log.debug("过滤后: {} {}", topic, asynResult);
        } catch (Exception e) {
            log.error("analysis: ", e);
        }
    }

    public static List<String> runaEquValveStatusFaults(String status) {
        ArrayList<String> list = new ArrayList<>();
        int hexCode = Integer.parseInt(status, 16);
        if ((hexCode >> 8 & 0b11) == 0b11) {
            list.add("阀门异常");
        }
        if ((hexCode >> 14 & 1) == 1) {
            list.add("阀体分离");
        }
        if ((hexCode >> 15 & 1) == 1) {
//            list.add("无线异常");
        }
        return list;
    }


    public static List<String> runaDeviceBuildValveFaults(int status, String model) {
        ArrayList<String> list = new ArrayList<>();
        if ((status >> 12 & 1) == 1) {
            addValveSeparateWithBody(list, model);
        }
        if ((status >> 10 & 1) == 1) {
            list.add("蓝牙故障");
        }
        if ((status >> 4 & 1) == 1) {
            list.add("楼阀堵转");
        }
        if ((status >> 15 & 1) == 1) {
            list.add("开度未标定");
        }
        return list;
    }

    public static List<String> runaDeviceBuildValveFaults(int status) {
        ArrayList<String> list = new ArrayList<>();
        if ((status >> 12 & 1) == 1) {
            list.add("阀体分离");
        }
        if ((status >> 10 & 1) == 1) {
            list.add("蓝牙故障");
        }
        if ((status >> 4 & 1) == 1) {
            list.add("楼阀堵转");
        }
        if ((status >> 15 & 1) == 1) {
            list.add("开度未标定");
        }
        return list;
    }

    public static List<String> novoconValveFaults(int v) {
        List<String> faults = new ArrayList<>();
        if (v >> 7 == 1) {
            faults.add("阀体分离");
        }
        if ((v >> 5 & 1) == 1 || (v >> 6 & 1) == 1) {
            faults.add("阀门异常");
        }
        if ((v >> 2 & 1) == 1) {
            faults.add("无线异常");
        }
        return faults;
    }

    public static List<String> beaconValveFaults(int v) {
        switch(v) {
            case 1:
                return Collections.singletonList("阀体分离");
            case 2:
            case 3:
                return Collections.singletonList("阀门异常");
        }
        return null;
    }

    public static List<String> fkNbValveFaults(int v) {
        ArrayList<String> list = new ArrayList<>();
        if ((v >> 7 & 1) == 1) {
            list.add("电池欠压");
        }
        if ((v >> 6 & 1) == 1) {
            list.add("阀体分离");
        }
        if ((v >> 5 & 1) == 1 || (v >> 1 & 1) == 1) {
            list.add("回水故障");
        }
        if ((v >> 4 & 1) == 1 || (v & 1) == 1) {
            list.add("进水故障");
        }
        return list;
    }







}
